package com.woniu.inlet.timer;

import com.woniu.adapter.MatApplicationOpenFein;
import com.woniu.outlet.dao.*;
import com.woniu.outlet.po.*;
import com.woniu.utils.SendEmail;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class
MaterialApplicationAcheduling {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private MaterialBomPoMapper materialBomPoMapper;

    @Autowired
    private InventoryInoutPoMapper inventoryInoutPoMapper;

    @Autowired
    private InventoryPoMapper inventoryPoMapper;

    @Autowired
    private MaterialApplicationMapper materialApplicationMapper;

    @Autowired
    private WorkerPoMapper workerPoMapper;

    @Autowired
    private SendEmail sendEmail;

    @Scheduled(fixedRate = 300000)
    public void doMaterialAppication(){
        RLock rLock = redissonClient.getLock("doMaterialAppication");  //获取一把锁对象，此时还没有加锁
        rLock.lock(300000L, TimeUnit.MILLISECONDS);  //加锁，其实就是设置一个key-value ,setnx(“hello”)
        //查询所有未领取物料订单,通过创建时间进行升序排列
        MaterialApplicationExample materialApplicationExample = new MaterialApplicationExample();
        MaterialApplicationExample.Criteria criteria1 = materialApplicationExample.createCriteria();
        criteria1.andSpare1EqualTo("未领取");
        List<MaterialApplication> materialApplications = materialApplicationMapper.selectByExample(materialApplicationExample);

        //标识符
        boolean flag;

        for (MaterialApplication materialApplication : materialApplications) {
            flag=true;
            //根据每个订单的code查询相应的订单详细，并用list封装在matApplicationByCode中
            //ResponseEntity<MaterialApplication> matApplicationByCode = matApplicationOpenFein.getMatApplicationByCode(materialApplication.getApplicationCode());

            MaterialApplication materialApplication1 = materialApplicationMapper.selectApp(materialApplication.getApplicationCode());

            List<ApplicationDetail> list = new ArrayList<>();
            list = materialApplicationMapper.selectMaterialByCode(materialApplication.getApplicationCode());
            materialApplication1.setApplicationDetails(list);

            //MaterialApplication materialApplication1 = matApplicationByCode.getBody();

            //获取订单创建人的信息
            Integer userId = materialApplication.getUserId();
            WorkerPo workerPo = workerPoMapper.selectByPrimaryKey(Long.parseLong(userId+""));

            //获取每个订单所有的详细
            List<ApplicationDetail> applicationDetails = materialApplication1.getApplicationDetails();
            String msg="";
            String msg1="";
            String msg2="";
            for (ApplicationDetail applicationDetail : applicationDetails) {

                //在库存中查询每个原料的现有库存
                InventoryPoExample inventoryPoExample = new InventoryPoExample();
                InventoryPoExample.Criteria criteria2 = inventoryPoExample.createCriteria();
                criteria2.andCodeEqualTo(applicationDetail.getMaterialCode());
                List<InventoryPo> inventoryPos = inventoryPoMapper.selectByExample(inventoryPoExample);
                if(inventoryPos.size()==0){
                    flag=false;
                    msg1=msg1+applicationDetail.getMaterialCode()+"/";
                    msg2=msg2+applicationDetail.getMaterialCode()+"/";
                }else {
                    InventoryPo inventoryPo=inventoryPos.get(0);
                    if(inventoryPo.getNum()<applicationDetail.getApplicationNum()){
                        flag=false;
                        //只要有一个详细库存打不够就发送消息通知订单创建人
                        msg=msg+inventoryPo.getCode()+"-"+inventoryPo.getName()+"/";
                        msg2=msg2+applicationDetail.getMaterialCode()+"/";

                    }
                }

            }

            if(!flag){
                sendEmail.sendMsg("采购缺货通知:","库存不足的物料有:"+msg+"没有库存的物料为："+msg1,"2876105102@qq.com");
                sendEmail.sendMsg("原料申请通知","您的物料申请订单:"+msg2+"目前库存不足。",
                        workerPo.getEmail());
            }

            //在该订单所有详细库存都满足的情况下，进行原料出库操作
            if(flag){
                for (ApplicationDetail applicationDetail : applicationDetails) {
                    //通过详细储存的原料code查询每个原料的基本信息
                    MaterialBomPoExample materialBomPoExample = new MaterialBomPoExample();
                    MaterialBomPoExample.Criteria criteria = materialBomPoExample.createCriteria();
                    criteria.andMaterialCodeEqualTo(applicationDetail.getMaterialCode());
                    List<MaterialBomPo> materialBomPos = materialBomPoMapper.selectByExample(materialBomPoExample);
                    MaterialBomPo materialBomPo=materialBomPos.get(0);

                    //将原料的基本信息封装到inout对象中
                    InventoryInoutPo inventoryInoutPo = new InventoryInoutPo();

                    if(applicationDetail.getSpare2().equals("M")){
                        inventoryInoutPo.setMaterialBomId( Integer.parseInt(materialBomPo.getId()+""));
                    }

                    if(applicationDetail.getSpare2().equals("C")){
                        inventoryInoutPo.setComponentId( Integer.parseInt(materialBomPo.getId()+""));
                    }

                    inventoryInoutPo.setCode(materialBomPo.getMaterialCode());
                    inventoryInoutPo.setColor(materialBomPo.getColor());
                    inventoryInoutPo.setName(materialBomPo.getMaterialName());
                    inventoryInoutPo.setInoutWay("people");
                    inventoryInoutPo.setIsout("Y");
                    inventoryInoutPo.setInoutNum(applicationDetail.getApplicationNum());
                    inventoryInoutPo.setSpecificationModel(materialBomPo.getSpecificationModel());
                    inventoryInoutPo.setSpecificationUnit(materialBomPo.getSpecificationUnit());
                    inventoryInoutPo.setTheoreticalWeight(materialBomPo.getTheoreticalWeight());
                    inventoryInoutPo.setValuationUnit(materialBomPo.getValuationUnit());
                    inventoryInoutPo.setType(applicationDetail.getSpare2());
                    inventoryInoutPo.setCreateTime(new Date());

                    inventoryInoutPo.setCreateWorkerId(1);
                    inventoryInoutPo.setInoutType("O");
                    inventoryInoutPo.setMaterialApplicationId(applicationDetail.getApplicationId());
                    inventoryInoutPo.setMaterialApplicationCode(materialApplication.getApplicationCode());

                    //添加原料出入库详细
                    inventoryInoutPoMapper.insertSelective(inventoryInoutPo);

                    //修改相应原料的库存
                    inventoryPoMapper.materialOut(applicationDetail);

                    //发送通知给订单创建人
                    sendEmail.sendMsg("原料申请通知","您的物料申请订单:"+materialApplication.getApplicationCode()+"已备齐，请尽快领取。",
                            workerPo.getEmail());

                    //将订单状态修改为已备货
                    //matApplicationOpenFein.ready(materialApplication.getApplicationCode());

                    MaterialApplication materialApplication2 = new MaterialApplication();
                    materialApplication2.setSpare1("已备货");
                    materialApplication2.setApplicationCode(materialApplication.getApplicationCode());
                    materialApplicationMapper.ready(materialApplication2);
                }
            }
        }
    }
}
